//
//  UploadCenterCell.m
//  Rail
//
//  Created by hychen on 2018/2/25.
//  Copyright © 2018年 Hychen. All rights reserved.
//

#import "UploadCenterCell.h"
#import "RMDisplayLabel.h"
#import "RMDownloadIndicator.h"

@interface UploadCenterCell ()
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *iconView;
@property (weak, nonatomic) IBOutlet UILabel *titleLabel;
@property (weak, nonatomic) IBOutlet UILabel *descLabel;
@property (weak, nonatomic) IBOutlet UIView *circleView;
@property (weak, nonatomic) IBOutlet UILabel *statusLabel;

@property (nonatomic, strong) RMDisplayLabel *circleLabel;
@property (nonatomic, strong) RMDownloadIndicator *circle;
@end
@implementation UploadCenterCell

- (void)awakeFromNib {
    [super awakeFromNib];
    
    self.circle = [[RMDownloadIndicator alloc] initWithFrame:CGRectMake(0, 0, 35, 35) type:kRMClosedIndicator];
    [self.circle setBackgroundColor:[UIColor whiteColor]];
    [self.circle setStrokeColor:Color_RGB(226, 230, 235)];
    self.circle.radiusPercent = 0.45;
    [self.circleView addSubview:self.circle];
    [self.circle loadIndicator];
    
    self.circleLabel = [RMDisplayLabel new];
    self.circleLabel.frame = CGRectMake(0, 10, 35, 15);
    [self.circleLabel updateValue:0];
    [self.circleView addSubview:self.circleLabel];
    
    [self setUploadStatus:TLUploadStatusWait];
    [self setUploadPercent:0.f];
}

- (void)setSelected:(BOOL)selected animated:(BOOL)animated {
    [super setSelected:selected animated:animated];

    // Configure the view for the selected state
}

- (void)setUploadStatus:(TLUploadStatus)status
{
    UIColor *fillColor = nil;
    NSString *statusTitle = nil;
    switch (status) {
        case TLUploadStatusUploading:
        {
            statusTitle = @"正在上传";
            fillColor = Color_RGB(54, 222, 145);
            break;
        }
        case TLUploadStatusStop:
        {
            statusTitle = @"已暂停";
            fillColor = Color_RGB(255, 173, 34);
            break;
        }
        case TLUploadStatusFailure:
        {
            statusTitle = @"上传失败";
            fillColor = Color_RGB(255, 64, 38);
            break;
        }
        case TLUploadStatusWait:
        default:
        {
            statusTitle = @"等待上传";
            fillColor = Color_RGB(167, 167, 167);
        }
            break;
    }

    self.statusLabel.text = statusTitle;
    self.circleLabel.textColor = fillColor;
    [self.circle setFillColor:fillColor];
}

- (void)setUploadPercent:(CGFloat)percent
{
    if (percent > 1.f) {
        percent = 1.f;
    }
    else if (percent < 0.f) {
        percent = 0.f;
    }
    
    [self.circleLabel updateValue:percent];
    
    [self.circle updateWithTotalBytes:1.f downloadedBytes:percent];
}
@end
